package com.lark.web.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.lark.common.enums.HostStatus;
import com.lark.common.enums.HostType;
import com.lark.common.exception.CustomException;
import com.lark.common.utils.StringUtils;
import com.lark.web.docker.service.DockerUtil;
import com.github.dockerjava.api.DockerClient;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.lark.common.core.domain.BaseEntity;
import com.lark.common.core.service.BaseServiceImpl;
import com.lark.web.mapper.HostMapper;
import com.lark.web.domain.Host;
import com.lark.web.service.HostService;
import org.springframework.transaction.annotation.Transactional;
import com.lark.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 主机Service业务层处理
 *
 * @author fei
 * @email 952597665@qq.com
 * @date 2024-08-10
 */
@Service
@Transactional(readOnly = true)
public class HostServiceImpl extends BaseServiceImpl<HostMapper, Host> implements HostService {

    private static final Logger log = LoggerFactory.getLogger(HostServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param host 主机
     * @return 主机
     */
    @Override
    public Host get(Host host) {
        Host dto = super.get(host);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 主机id
     * @return 主机
     */
    @Override
    public Host get(String id) {
        Host dto = super.get(id);
        return dto;
    }

    /**
     * 查询主机列表
     *
     * @param host 主机
     * @return 主机
     */
    @Override
    public List<Host> findList(Host host) {
        List<Host> hostList = super.findList(host);
        return hostList;
    }

    /**
     * 分页查询主机列表
     *
     * @param host 主机
     * @return 主机
     */
    @Override
    public PageInfo<Host> findPage(Host host) {
        PageInfo<Host> page = super.findPage(host);
        return page;
    }

    /**
     * 保存主机
     *
     * @param host
     * @return 结果
     */
    @Override
    public boolean save(Host host) {
        checkHostIpUnique(host);
        return super.save(host);
    }

    /**
     * 校验主机IP的唯一性
     *
     * @param host
     */
    @Override
    public void checkHostIpUnique(Host host) {
        Host hostUnique = new Host();
        hostUnique.setNotEqualId(host.getId());
        hostUnique.setHostIp(host.getHostIp());
        if (!CollectionUtils.isEmpty(mapper.findListWithUnique(hostUnique))) {
            throw new BizException("主机IP已存在，请重新输入！");
        }
    }

    /**
     * 删除主机信息
     *
     * @param host
     * @return 结果
     */
    @Override
    public boolean remove(Host host) {
        return super.remove(host);
    }

    /**
     * 批量删除主机
     *
     * @param ids 需要删除的主机ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteHostByIds(String[] ids) {
        return mapper.deleteHostByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public DockerClient buildClient(String ip) {
        Host hostQuery = new Host();
        hostQuery.setHostIp(StringUtils.isBlank(ip)?"0.0.0.0":ip);
        List<Host> hostList = this.mapper.findList(hostQuery);
        if (CollectionUtils.isEmpty(hostList)) {
            throw new CustomException("请输入正确的主机IP！");
        }
        Host host = hostList.get(0);
        DockerClient client = null;
        if (Objects.equals(HostType.LOCAL.getCode(), host.getType())) {
            client = DockerUtil.createClient();
        } else if (Objects.equals(HostType.REMOTE.getCode(), host.getType())) {
            client = DockerUtil.createClient(host.getHostIp(), host.getHostPort());
        }
        return client;
    }

    @Override
    public List<Host> getEnalbeList(String ip) {
        Host hostQuery = new Host();
        hostQuery.setHostIp(ip);
        hostQuery.setStatus(HostStatus.ENABLE.getCode());
        List<Host> hostList = this.mapper.findList(hostQuery);
        return hostList;
    }

}
